#-------------------------------------------------------------------------------
# Foundation
#-------------------------------------------------------------------------------

if(FIPS_LINUX OR FIPS_MACOS)
    FIND_PACKAGE(CURL)
endif()

if (EXISTS "${NROOT}/syswork/export.zip")
    create_resource("${NROOT}/syswork/" "${NROOT}/syswork/export.zip" system_resources)
    set_target_properties(system_resources PROPERTIES FOLDER Resources)
    set_target_properties(system_resources-res PROPERTIES FOLDER Resources)
endif()

nebula_begin_module(foundation)
    
    target_precompile_headers(foundation PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/foundation/stdneb.h ${CMAKE_CURRENT_SOURCE_DIR}/core/ptr.h ${CMAKE_CURRENT_SOURCE_DIR}/core/rtti.h ${CMAKE_CURRENT_SOURCE_DIR}/core/refcounted.h)
    if(FIPS_LINUX)
        fips_libs(uuid stdc++fs)
    endif()
    target_include_directories(foundation PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    fips_deps(nebula-libs zlib tinyxml nflatbuffer)
    if(TARGET system_resources)
        fips_deps(system_resources)
        target_compile_definitions(foundation PUBLIC HAS_EMBEDDED_EXPORT)
    endif()

    if(FIPS_WINDOWS)
        fips_libs(curl)
        fips_deps(StackWalker)
    else()
        if(CURL_FOUND)
            target_include_directories(foundation PRIVATE ${CURL_INCLUDE_DIR})
            fips_deps(${CURL_LIBRARY_RELEASE})
        else()
            MESSAGE(FATAL_ERROR "libcurl not found!")
        endif()
    endif()

        fips_files(foundation/stdneb.h)
        fips_dir(core)
            fips_files(
                config.h
                coreserver.cc
                coreserver.h
                cvar.cc
                cvar.h
                debug.cc
                debug.h
                exithandler.cc
                exithandler.h
                factory.cc
                factory.h
                ptr.h
                refcounted.cc
                refcounted.h
                refcountedlist.cc
                refcountedlist.h
                rtti.cc
                rtti.h
                rttimacros.h
                singleton.h
                simd.h
                sysfunc.h
                types.h
                weakptr.h
                debug/corepagehandler.cc
                debug/corepagehandler.h
                debug/stringatompagehandler.cc
                debug/stringatompagehandler.h
            )
        fips_dir(debug)
        fips_files(
            debugcounter.cc
            debugcounter.h
            debugfloat.h
            debughandler.cc
            debughandler.h
            debuginterface.cc
            debuginterface.h
            debugpagehandler.cc
            debugpagehandler.h
            debugserver.cc
            debugserver.h
            debugtimer.cc
            debugtimer.h
            minidump.h
            stacktrace.h
        )
        fips_dir(util)
        fips_files(
            array.h
            arrayallocator.h
            arrayallocatorsafe.h
            arraystack.h
            arrayqueue.h
            bit.h
            bitfield.h
            blob.cc
            blob.h
            bvh.h
            color.h
            color.cc
            commandlineargs.cc
            commandlineargs.h
            crc.cc
            crc.h
            delegate.h
            dictionary.h
            fixedarray.h
            fixedtable.h
            fixedpool.h
            fourcc.h
            globalstringatomtable.cc
            globalstringatomtable.h
            guid.h
            hashtable.h
            keyvaluepair.h
            list.h
            localstringatomtable.cc
            localstringatomtable.h
            pinnedarray.h
            priorityarray.h
            quadtree.h
            queue.h
            random.h
            random.cc
            randomnumbertable.cc
            randomnumbertable.h
            ringbuffer.h
            round.h
            runlengthcodec.cc
            runlengthcodec.h
            set.h
            simpletree.h
            sparsetable.h
            stack.h
            string.cc
            string.h
            stringatom.cc
            stringatom.h
            stringatomtablebase.cc
            stringatomtablebase.h
            stringbuffer.cc
            stringbuffer.h
            trivialarray.h
            typepunning.h
            variant.h
            tupleutility.h
        )
        fips_dir(interface)
        fips_files(
            interfacebase.cc
            interfacebase.h
            interfacehandlerbase.cc
            interfacehandlerbase.h
        )
        fips_dir(io)
        fips_files(
            assign.h
            assignregistry.cc
            assignregistry.h
            binaryreader.cc
            binaryreader.h
            binarywriter.cc
            binarywriter.h
            bxmlreader.cc
            bxmlreader.h
            console.cc
            console.h
            consolehandler.cc
            consolehandler.h
            excelxmlreader.cc
            excelxmlreader.h
            filestream.cc
            filestream.h
            safefilestream.cc
            safefilestream.h
            filetime.h
            fswrapper.h
            gamecontentserver.cc
            gamecontentserver.h
            historyconsolehandler.cc
            historyconsolehandler.h
            ioserver.cc
            ioserver.h
            iointerface.cc
            iointerface.h
            iointerfacehandler.cc
            iointerfacehandler.h
            logfileconsolehandler.cc
            logfileconsolehandler.h
            jsonreader.cc
            jsonreader.h
            jsonwriter.cc
            jsonwriter.h
            filedialog.cc
            filedialog.h
            filewatcher.cc
            filewatcher.h
            mediatype.h
            embeddedmemorystream.cc
            embeddedmemorystream.h
            memorystream.cc
            memorystream.h
            schemeregistry.cc
            schemeregistry.h
            stream.cc
            stream.h
            streamreader.cc
            streamreader.h
            streamwriter.cc
            streamwriter.h
            textreader.cc
            textreader.h
            textwriter.cc
            textwriter.h
            uri.cc
            uri.h
            xmlreader.cc
            xmlreader.h
            xmlwriter.cc
            xmlwriter.h
            util/bxmlfilestructs.h
            util/bxmlloaderutil.cc
            util/bxmlloaderutil.h
            #fixme. these have to be nidld
            iointerfaceprotocol.cc
        )
        fips_dir(io/zipfs)
        fips_files(ziparchive.cc
            ziparchive.h
            zipdirentry.cc
            zipdirentry.h
            zipfileentry.cc
            zipfileentry.h
            zipfilestream.cc
            zipfilestream.h
            zipfilesystem.cc
            zipfilesystem.h
            ionebula3.cc
            ionebula3.h
        )
        fips_dir(io/archfs)
        fips_files(
            archive.cc
            archive.h
            archivebase.cc
            archivebase.h
            archivefilesystem.cc
            archivefilesystem.h
            archivefilesystembase.cc
            archivefilesystembase.h
        )
        fips_dir(io/cache)
        fips_files(
            streamcache.cc
            streamcache.h
            cachedstream.cc
            cachedstream.h
            cachedstreamtypes.cc
            cachedstreamtypes.h
        )
        fips_dir(io/debug)
        fips_files(
            consolepagehandler.cc
            consolepagehandler.h
            iopagehandler.cc
            iopagehandler.h
        )
        fips_dir(io/base)
        fips_files(
            gamecontentserverbase.cc
            gamecontentserverbase.h
        )
        fips_dir(math)
        fips_files(
            angularpfeedbackloop.h
            bbox.cc
            bbox.h
            clipstatus.h
            curves.cc
            curves.h
            euler.h
            extrapolator.h
            frustum.h
            half.h
            line.cc
            line.h
            mat4.cc
            mat4.h
            noise.h
            pfeedbackloop.h
            pidfeedbackloop.h
            plane.h
            point.h
            polar.h
            quat.cc
            quat.h
            rectangle.h
            scalar.h
            sphere.cc
            sphere.h
            sse.h
            transform.h
            transform44.h
            vec2.h
            vec3.h
            vec4.cc
            vec4.h
            vector.h
        )
        fips_dir(memory)
        fips_files(
            arenaallocator.h
            heap.h
            memory.cc
            memory.h
            memorypool.h
            poolarrayallocator.cc
            poolarrayallocator.h
            rangeallocator.h
            ringallocator.h
            debug/memorypagehandler.cc
            debug/memorypagehandler.h
        )
        fips_dir(messaging)
        fips_files(
            asyncport.cc
            asyncport.h
            batchmessage.cc
            batchmessage.h
            blockinghandlerthread.cc
            blockinghandlerthread.h
            delegatetable.cc
            delegatetable.h
            dispatcher.cc
            dispatcher.h
            handler.cc
            handler.h
            handlerthreadbase.cc
            handlerthreadbase.h
            id.h
            message.cc
            message.h
            messagereader.cc
            messagereader.h
            messagewriter.cc
            messagewriter.h
            messagecallbackhandler.h
            messagecallbackhandler.cc
            port.cc
            port.h
            runthroughhandlerthread.cc
            runthroughhandlerthread.h
            staticmessagehandler.cc
            staticmessagehandler.h
        )
        fips_dir(threading)
        fips_files(
            assertingmutex.cc
            assertingmutex.h
            barrier.h
            criticalsection.h
            event.h
            interlocked.h
            lockfreequeue.h
            objectref.cc
            objectref.h
            readwritelock.h
            safeflag.h
            safepriorityqueue.h
            safequeue.h
            spinlock.h
            thread.cc
            thread.h
            threadbarrier.h
            threadid.h
            debug/threadpagehandler.cc
            debug/threadpagehandler.h
        )
        fips_dir(timing)
        fips_files(
            calendartime.h
            time.h
            timer.h
            base/calendartimebase.cc
            base/calendartimebase.h
        )
        fips_dir(net)
        fips_files(
            debugmessage.h
            debugpacket.cc
            debugpacket.h
            messageclient.cc
            messageclient.h
            messageclientconnection.cc
            messageclientconnection.h
            tcpclient.cc
            tcpclient.h
            tcpclientconnection.cc
            tcpclientconnection.h
            tcpmessagecodec.cc
            tcpmessagecodec.h
            tcpserver.cc
            tcpserver.h
            socket/ipaddress.h
            socket/socket.cc
            socket/socket.h
            tcp/stdtcpclient.cc
            tcp/stdtcpclient.h
            tcp/stdtcpclientconnection.cc
            tcp/stdtcpclientconnection.h
            tcp/stdtcpserver.cc
            tcp/stdtcpserver.h
        )
        fips_dir(profiling)
        fips_files(
            profiling.cc
            profiling.h
        )
        fips_dir(system)
        fips_files(
            appentry.h
            byteorder.h
            cpu.h
            process.h
            library.h
            systeminfo.h
            nebulasettings.h
            base/processbase.h
            base/librarybase.h
        )
        fips_dir(app)
        fips_files(
            application.cc
            application.h
            consoleapplication.cc
            consoleapplication.h
        )
        fips_dir(http)
        fips_files(
            defaulthttprequesthandler.cc
            defaulthttprequesthandler.h
            httpclient.cc
            httpclient.h
            httpclientregistry.cc
            httpclientregistry.h
            httpmethod.h
            httpnzstream.cc
            httpnzstream.h
            httprequest.cc
            httprequest.h
            httprequesthandler.cc
            httprequesthandler.h
            httprequestreader.cc
            httprequestreader.h
            httprequestwriter.cc
            httprequestwriter.h
            httpresponsereader.cc
            httpresponsereader.h
            httpresponsewriter.cc
            httpresponsewriter.h
            httpserver.cc
            httpserver.h
            httpserverproxy.cc
            httpserverproxy.h
            httpstatus.cc
            httpstatus.h
            httpstream.cc
            httpstream.h
            httpinterface.h
            httpinterface.cc
            httpmessagehandler.h
            httpmessagehandler.cc
            httpprotocol.cc
            httpprotocol.h
        )
        fips_dir(http/curl)
        fips_files(
            curlhttpclient.cc
            curlhttpclient.h
        )
        fips_dir(http/debug)
        fips_files(
            helloworldrequesthandler.cc
            helloworldrequesthandler.h
            svgtestpagehandler.cc
            svgtestpagehandler.h
        )
        fips_dir(http/html)
        fips_files(
            htmlelement.cc
            htmlelement.h
            htmlpagewriter.cc
            htmlpagewriter.h
        )
        fips_dir(http/svg)
        fips_files(
            svglinechartwriter.cc
            svglinechartwriter.h
            svgpagewriter.cc
            svgpagewriter.h
        )
        fips_dir(ids)
        fips_files(
            id.h
            idallocator.h
            idpool.h
            idgenerationpool.cc
            idgenerationpool.h
        )
        fips_dir(jobs)
        fips_files(
            jobs.cc
            jobs.h
        )
        fips_dir(jobs2)
        fips_files(
            jobs2.cc
            jobs2.h
        )
        fips_dir(fibers)
        fips_files(
            fiber.h
            fibers.cc
            fibers.h
        )
        fips_dir(framesync)
        fips_files(
            framesynctimer.cc
            framesynctimer.h
        )
    fips_dir(.)
    if (FIPS_WINDOWS)
        fips_files(foundation.natvis foundation.natstepfilter)
        fips_dir(core GROUP "core/win32")
            fips_files(
                win32/precompiled.h
                win32/win32singleton.h
                win32/win32sysfunc.cc
                win32/win32sysfunc.h
            )
        fips_dir(memory GROUP "memory/win32")
            fips_files(
                win32/winmemory.h
                win32/win32heap.cc
                win32/win32heap.h
                win32/win32memory.cc
                win32/win32memory.h
                win32/win32memoryconfig.cc
                win32/win32memoryconfig.h
                win32/win32memorypool.cc
                win32/win32memorypool.h
            )
        fips_dir(debug GROUP "debug/win32")
            fips_files(
                win32/win32minidump.cc
                win32/win32minidump.h
                win32/win32stacktrace.cc
                win32/win32stacktrace.h
            )
        fips_dir(util GROUP "util/win32")
            fips_files(
                win32/win32guid.cc
                win32/win32guid.h
                win32/win32stringconverter.cc
                win32/win32stringconverter.h
            )
        fips_dir(io GROUP "io/win32")
            fips_files(
                win32/win32consolehandler.cc
                win32/win32consolehandler.h
                win32/win32filewatcher.cc
                win32/win32filewatcher.h
                win32/win32filetime.cc
                win32/win32filetime.h
                win32/win32fswrapper.cc
                win32/win32fswrapper.h
            )
        fips_dir(timing GROUP "timing/win32")
            fips_files(
                win32/win32calendartime.cc
                win32/win32calendartime.h
                win32/win32timer.cc
                win32/win32timer.h
            )
        fips_dir(net GROUP "net/win32")
            fips_files(
                win32/win32ipaddress.cc
                win32/win32ipaddress.h
                win32/win32socket.cc
                win32/win32socket.h
            )
        fips_dir(system GROUP "system/win32")
            fips_files(
                win32/win32cpu.h
                win32/win32environment.cc
                win32/win32environment.h
                win32/win32process.cc
                win32/win32process.h
                win32/win32library.cc
                win32/win32library.h
                win32/win32registry.cc
                win32/win32registry.h
                win32/win32systeminfo.cc
                win32/win32systeminfo.h
            )
        fips_dir(fibers GROUP "fibers/win32")
            fips_files(
                win32/win32fiber.cc
            )
        fips_dir(.)
    endif()
    if (FIPS_POSIX)
        fips_files(
            core/posix/precompiled.h
            core/posix/posixsingleton.h
            core/posix/posixsysfunc.cc
            core/posix/posixsysfunc.h
            fibers/posix/posixfiber.cc
            memory/posix/posixmemory.h
            memory/posix/posixheap.cc
            memory/posix/posixheap.h
            memory/posix/posixmemory.cc
            memory/posix/posixmemory.h
            memory/posix/posixmemoryconfig.cc
            memory/posix/posixmemoryconfig.h
            memory/posix/posixmemorypool.cc
            util/posix/posixguid.cc
            util/posix/posixguid.h
            io/posix/posixconsolehandler.cc
            io/posix/posixconsolehandler.h
            io/posix/posixfiletime.cc
            io/posix/posixfiletime.h
            io/posix/linuxfilewatcher.cc
            io/posix/linuxfilewatcher.h
            io/posix/posixfswrapper.cc
            io/posix/posixfswrapper.h
            timing/posix/posixtimer.cc
            timing/posix/posixtimer.h
            timing/posix/posixcalendartime.cc
            timing/posix/posixcalendartime.h
            threading/posix/posixreadwritelock.cc
            threading/posix/posixreadwritelock.h
            net/posix/posixipaddress.cc
            net/posix/posixipaddress.h
            net/posix/posixsocket.cc
            net/posix/posixsocket.h
            system/posix/posixcpu.h
            system/posix/posixsysteminfo.h
            system/posix/posixsysteminfo.cc
            system/posix/posixenvironment.cc
            system/posix/posixenvironment.h
            system/posix/posixsettings.cc
            system/posix/posixsettings.h
        )
    endif()
    fips_dir(.)

    if(FIPS_WINDOWS)
        fips_dir(threading/win32 GROUP "threading/win32")
        fips_files(
            win32barrier.h
            win32criticalsection.cc
            win32criticalsection.h
            win32event.h
            win32interlocked.cc
            win32readwritelock.cc
            win32readwritelock.h
            win32thread.cc
            win32thread.h
            win32threadbarrier.h
            win32threadid.h
        )
    elseif(FIPS_LINUX)
        fips_dir(threading/linux GROUP "threading/linux")
        fips_files(
            linuxevent.h
            linuxthread.cc
            linuxthread.h
        )
        fips_dir(threading/gcc GROUP "threading/gcc")
        fips_files(gccinterlocked.cc)
    else()
        fips_dir(threading/posix GROUP "threading/posix")
        fips_files(
            posixbarrier.h
            posixcriticalsection.h
            posixevent.h

            posixthread.cc
            posixthread.h
        )
        fips_dir(threading/gcc GROUP "threading/gcc")
        fips_files(gccinterlocked.cc)
    endif()
nebula_end_module()
#FIXME cant add python include dir globally as it clashes with antlr4
target_include_directories(foundation PUBLIC ${PYTHON_INCLUDE_DIRS})
